package com.mango.coomb.threads;


import com.mango.coomb.constant.ThreadConst;
import com.mango.coomb.data.IOData;
import com.mango.coomb.enums.AreaClass;
import com.mango.coomb.enums.Sign;
import com.mango.coomb.enums.Status;
import com.mango.coomb.threads.parameter.ThreadParameter;
import com.mango.coomb.threads.parameter.util.ThreadParamUpdateUtil;

public class ReaderNegWrite extends Thread {
    private static final Integer[] readerNegBufferValueArr = ThreadParameter.getReaderNegBufferValueArr();
    private Status status = Status.NOT_BEGIN;
    private boolean paused=false;
    @Override
    public void run() {
        while (ThreadParameter.isReaderNegWriteRunning) {
            while (!this.paused) {
                Integer value = null;
                int i = ThreadParamUpdateUtil.searchNotNull(AreaClass.READER, Sign.READER_NEG);
                if (i == ThreadConst.NOT_SEARCH) {
                    this.status = Status.PAUSED;
                    try {
                        synchronized (readerNegBufferValueArr) {
                            readerNegBufferValueArr.wait();
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    this.status = Status.PAUSED;
                    synchronized (ThreadParameter.readerNegBufferLock[i]) {
                        this.status = Status.RUNNING;
                        ThreadParameter.isReaderNegBufferLockedArr[i] = true;
                        try {
                            Thread.sleep(500L);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        value = readerNegBufferValueArr[i];
                        ThreadParamUpdateUtil.updateReaderNegBufferValueArr(i, null);
                        ThreadParameter.isReaderNegBufferLockedArr[i] = false;
                        this.status = Status.NOT_BEGIN;
                    }
                    IOData.TargetNegDataList.add(value);
                    synchronized (readerNegBufferValueArr) {
                        readerNegBufferValueArr.notifyAll();
                    }
                }
            }
//            this.status = Status.PAUSED;
            try {
                synchronized (ThreadParameter.readerNegWriteLock) {
                    ThreadParameter.readerNegWriteLock.wait();
                }
//                synchronized (ThreadParameter.totalLock){
//                    ThreadParameter.totalLock.wait();
//                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
//            this.status = Status.NOT_BEGIN;
        }
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public boolean isPaused() {
        return paused;
    }

    public void setPaused(boolean paused) {
        this.paused = paused;
    }
}
